﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using Simple.Common.Core.Log;

namespace Simple.Common.Core.Mail
{
    public class SimpleMailHelper
    {
        /// <summary>
        /// 发送邮件，不使用SSL协议加密
        /// </summary>
        /// <param name="mailTitle">邮件标题</param>
        /// <param name="mailContent">邮件内容</param>
        /// <param name="mailAddress">收件人地址集合</param>
        public static void SendMail(string mailTitle, string mailContent, params string[] mailAddress)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            //添加收件人
            foreach (string address in mailAddress)
            {
                msg.To.Add(address);
            }
            //设置发件邮箱地址，发件人姓名，以及邮件编码
            msg.From = new MailAddress("yourmail@163.com", "Sender", System.Text.Encoding.UTF8);
            //设置邮件标题
            msg.Subject = mailTitle;
            //设置邮件标题编码
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            //设置邮件内容
            msg.Body = mailContent;
            //设置邮件内容编码
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            //指定是否是HTML邮件
            msg.IsBodyHtml = false;
            //设置邮件优先级
            msg.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();
            //设置邮箱账号密码
            client.Credentials = new System.Net.NetworkCredential("yourmail@163.com", "password");
            //设置smtp服务器
            client.Host = "smtp.163.com";
            object userState = msg;
            try
            {
                //异步发送
                //client.SendAsync(msg, userState);
                //同步发送
                client.Send(msg);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                SimpleLogHelper.Error("发送邮件失败", ex);
            }
        }

        /// <summary>
        /// 发送邮件，使用SSL协议加密
        /// </summary>
        /// <param name="mailTitle">邮件标题</param>
        /// <param name="mailContent">邮件内容</param>
        /// <param name="mailAddress">收件人地址集合</param>
        public static void SendMailSSL(string mailTitle, string mailContent, params string[] mailAddress)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            //添加收件人
            foreach (string address in mailAddress)
            {
                msg.To.Add(address);
            }
            //设置发件邮箱地址，发件人姓名，以及邮件编码
            msg.From = new MailAddress("yourmail@163.com", "Sender", System.Text.Encoding.UTF8);
            //设置邮件标题
            msg.Subject = mailTitle;
            //设置邮件标题编码
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            //设置邮件内容
            msg.Body = mailContent;
            //设置邮件内容编码
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            //指定是否是HTML邮件
            msg.IsBodyHtml = false;
            //设置邮件优先级
            msg.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();
            //设置邮箱账号密码
            client.Credentials = new System.Net.NetworkCredential("yourmail@163.com", "password");
            //设置邮箱服务器使用的SSL协议端口 -可能个别要见服务器要求设置SSL协议端口
            //client.Port = 25;
            //设置smtp服务器
            client.Host = "smtp.163.com";
            //使用ssl加密
            client.EnableSsl = true;
            object userState = msg;
            try
            {
                //异步发送
                //client.SendAsync(msg, userState);
                //同步发送
                client.Send(msg);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                SimpleLogHelper.Error("发送邮件失败", ex);
            }
        }
    }
}
